package com.ruiysoft.service.sys.redis.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.constant.ConstantKey;
import com.ruiysoft.entity.sys.SysShopPayPlatform;
import com.ruiysoft.entity.sys.SysShopPersonnel;
import com.ruiysoft.entity.sys.SysUser;
import com.ruiysoft.entity.sys.SysUserShopRelation;
import com.ruiysoft.service.sys.ISysShopPayPlatformService;
import com.ruiysoft.service.sys.ISysShopPersonnelService;
import com.ruiysoft.service.sys.ISysUserService;
import com.ruiysoft.service.sys.ISysUserShopRelationService;
import com.ruiysoft.service.sys.redis.IRedisSysUserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by 化增光 on 2018/10/22.
 */
@Service
public class RedisSysUserServiceImpl implements IRedisSysUserService {
    @Resource
    private RedisTemplate<String,SysShopPersonnel> personnelRedisTemplate;

    @Resource
    private RedisTemplate<String,SysUser> userRedisTemplate;

    @Resource
    private RedisTemplate<String,Long> idRedisTemplate;

    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private ISysShopPersonnelService iSysShopPersonnelService;

    @Resource
    private RedisTemplate<String,SysShopPayPlatform> platformRedisTemplate;

    @Resource
    private ISysShopPayPlatformService iSysShopPayPlatformService;

    @Resource
    private ISysUserShopRelationService iSysUserShopRelationService;

    @Override
    public void setCurrentLoginShop(int loginType,Long userId,Long personnelId, Long shopId) {
        String key = getKey(userId,personnelId,loginType);
        idRedisTemplate.opsForValue().set(ConstantKey.SYS_MIC_LOGIN_USER_SHOP_KEY+key,shopId);
    }

    private String getKey(Long userId, Long personnelId,int loginType) {
        String key1 = "N/A";
        String key2 = "N/A";
        if (null != userId){
            key1 = userId.toString();
        }
        if (null != personnelId){
            key2 = personnelId.toString();
        }
        StringBuffer sb = new StringBuffer();
        sb.append(key1).append("_").append(key2).append("_").append(loginType);
        return sb.toString();
    }

    @Override
    public Long getCurrentLoginOnShopId(int loginType,Long userId,Long personnelId) {
        String key = getKey(userId,personnelId,loginType);
        return idRedisTemplate.opsForValue().get(ConstantKey.SYS_MIC_LOGIN_USER_SHOP_KEY+key);
    }

    @Override
    public SysUser selectSysUserByUserName(String username) {
        Long userId = idRedisTemplate.opsForValue().get(ConstantKey.SYS_USER_NAME_KEY+username);
        if (null == userId){
            SysUser sysUser = iSysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name",username));
            if (null != sysUser){
                idRedisTemplate.opsForValue().set(ConstantKey.SYS_USER_NAME_KEY+username,sysUser.getId());
            }
            return sysUser;
        }else{
            return selectSysUserById(userId);
        }
    }

    @Override
    public SysShopPersonnel selectSysShopPersonnelById(Long personnelId) {
        SysShopPersonnel personnel = personnelRedisTemplate.opsForValue().get(ConstantKey.SYS_PERSONNEL_ID_KEY+personnelId);
        if (null == personnel){
            personnel = iSysShopPersonnelService.getById(personnelId);
            if (null != personnel){
                personnelRedisTemplate.opsForValue().set(ConstantKey.SYS_PERSONNEL_ID_KEY+personnelId,personnel);
            }
        }
        return personnel;
    }

    @Override
    public SysShopPersonnel selectSysShopPersonnelByAppIdAndOpenId(String appId, String openid) {
        SysShopPersonnel personnel = null;
        Long personnelId = idRedisTemplate.opsForValue().get(ConstantKey.SYS_PERSONNEL_APPID_OPENID_KEY+appId+openid);
        if (null == personnelId){
            personnel = iSysShopPersonnelService.getOne(new QueryWrapper<SysShopPersonnel>().eq("app_id",appId).eq("openid",openid));
            if (null != personnel){
                idRedisTemplate.opsForValue().set(ConstantKey.SYS_PERSONNEL_APPID_OPENID_KEY+appId+openid,personnel.getId());
            }
        }else{
            personnel = selectSysShopPersonnelById(personnelId);
        }
        return personnel;
    }

    @Override
    public SysUser selectSysUserByPersonnelId(Long personnelId) {
        SysUser user = null;
        Long userId = idRedisTemplate.opsForValue().get(ConstantKey.SYS_USER_PERSONNEL_ID_KEY+personnelId);
        if (null == userId){
            user = iSysUserService.getOne(new QueryWrapper<SysUser>().eq("personnel_id",personnelId));
            if (null != user){
                idRedisTemplate.opsForValue().set(ConstantKey.SYS_USER_PERSONNEL_ID_KEY+personnelId,user.getId());
            }
        }else{
            user = selectSysUserById(userId);
        }
        return user;
    }
    @Override
    public SysUser selectSysUserById(Long userId) {
        SysUser user = userRedisTemplate.opsForValue().get(ConstantKey.SYS_USER_ID_KEY+userId);
        if (null == user){
            user = iSysUserService.getById(userId);
            if (null != user){
                userRedisTemplate.opsForValue().set(ConstantKey.SYS_USER_ID_KEY+user.getId(),user);
            }
        }
        return user;
    }

    @Override
    public void clearSysShopPersonnelById(Long personnelId) {
        personnelRedisTemplate.delete(ConstantKey.SYS_PERSONNEL_ID_KEY+personnelId);
    }

    @Override
    public SysShopPayPlatform selectSysShopPayPlatformByAppId(String appId) {
        SysShopPayPlatform platform = platformRedisTemplate.opsForValue().get(ConstantKey.SYS_PLATFORM_APPID_KEY+appId);
        if (null == platform){
            platform = iSysShopPayPlatformService.getOne(new QueryWrapper<SysShopPayPlatform>().eq("app_id",appId));
            //暂时注释
//            if (null != platform){
//                platformRedisTemplate.opsForValue().set(ConstantKey.SYS_PLATFORM_APPID_KEY+appId,platform);
//            }
        }

        return platform;
    }

    @Override
    public void clearCacheByUserName(String username) {
        Long userId = idRedisTemplate.opsForValue().get(ConstantKey.SYS_USER_NAME_KEY+username);
        if (null != userId){
           userRedisTemplate.delete(ConstantKey.SYS_USER_ID_KEY+userId);
        }
        idRedisTemplate.delete(ConstantKey.SYS_USER_NAME_KEY+username);
    }

    @Override
    public void clearCacheById(Long userId, Long personnelId) {
        userRedisTemplate.delete(ConstantKey.SYS_USER_ID_KEY+userId);
        idRedisTemplate.delete(ConstantKey.SYS_USER_PERSONNEL_ID_KEY+personnelId);

    }

    @Override
    public SysUserShopRelation selectUserShopRelationByUserId(Long userId) {
        return iSysUserShopRelationService.selectUserShopRelationByUserId(userId);
    }

//    @Override
//    public void setUserLoginShop(Long userId, @Valid Long shopId) {
//        idRedisTemplate.opsForValue().set(ConstantKey.SYS_PC_LOGIN_USER_SHOP_KEY+userId,shopId);
//    }
//
//    @Override
//    public Long getUserLoginShop(Long userId) {
//        return idRedisTemplate.opsForValue().get(ConstantKey.SYS_PC_LOGIN_USER_SHOP_KEY+userId);
//    }

}
